package com.project.rfidpage.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.project.rfidpage.dto.BaseParam;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class BaseController {

    protected HttpServletRequest request;

    protected HttpServletResponse response;

    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    protected <E> Page<E> getPage(boolean page, @RequestBody BaseParam param) {
        String user = request.getHeader("user");
        param.setUid(user);
        if (page) {
            return PageHelper.startPage(param.getPageNum(), param.getPageSize());
        } else {
            return null;
        }
    }
}
